<?php
/**
 * 基础函数库
 * 
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0 alpha
 * @since 2011-05-03
 * @author monkee
 */

function avatar($user_id){
	return '/cache/avatar/'.$user_id.'.jpg';
}
function html($str){
	return htmlspecialchars($str);
}
function file_size($file_size){
	$file_size = floatval($file_size);
	$i = 0;
	while(($size = $file_size / 1024)>=1){
		$file_size = $size;
		$i ++;
	}
	$size = intval($file_size*100);
	$file_size = $size/100;
	switch($i){
		case 0 : return $file_size.'B';
		case 1 : return $file_size.'KB';
		case 2 : return $file_size.'MB';
		default : return $file_size;
	}
}

function simple_html($str){
	$str = html($str);
	$str = str_replace("\n", '<br />', $str);
	return $str;
}

function get_client_ip(){
	return $_SERVER['REMOTE_ADDR'];
}

function login_need(){
	global $core;
	$user = $core->current_user();
	if($user['user_id'] == 0){
		go_to('/user/login');
	}
}

function go_to($url, $message = ''){
	if($message){
		Cookie::set('message', $message);
	}
	Cookie::_set();
	echo '<script>top.location.href="'.$url.'";</script>';
	exit;
}

function get_avatar($user_id, $edit = false){
	$file = 'cache/avatar/'.$user_id.'.jpg';
	if(is_file(ROOT.$file) || $edit){
		return '/'.$file;
	}
	return '/image/no_avatar.jpg';
}